home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 / Ham Radio 2000.iso / ham2000 / satellit / vstsrc / vstdate1.c < prev    next >
C/C++ Source or Header  |  1995-01-23  |  9KB  |  249 lines

  1. /*
  2.  * %W% %E% %U%  [EXTREL_1.2]
  3.  *
  4.  * VersaTrack orbit calculations are based on those that appear in Dr. Manfred
  5.  * Bester's sattrack program (the Unix(tm) versions 1 and 2).
  6.  *
  7.  * The data from which the maps where generated come from "xsat", an
  8.  * X-Windows program by David A. Curry (N9MSW).
  9.  *
  10.  * Site coordinates come from various sources, including a couple of
  11.  * World Almanacs, and also from both of the programs mentioned above.
  12.  *
  13.  * The following are authors' applicable copyright notices:
  14.  *
  15.  *                                                                               
  16.  * Copyright (c) 1992, 1993, 1994 Manfred Bester. All Rights Reserved.        
  17.  *                                                                           
  18.  * Permission to use, copy, modify, and distribute this software and its      
  19.  * documentation for educational, research and non-profit purposes, without   
  20.  * fee, and without a written agreement is hereby granted, provided that the  
  21.  * above copyright notice and the following three paragraphs appear in all    
  22.  * copies.                                                                    
  23.  *                                                                              
  24.  * Permission to incorporate this software into commercial products may be    
  25.  * obtained from the author, Dr. Manfred Bester, 1636 M. L. King Jr. Way,     
  26.  * Berkeley, CA 94709, USA.                                                   
  27.  *                                                                             
  28.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,  
  29.  * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF    
  30.  * THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE AUTHOR HAS BEEN ADVISED   
  31.  * OF THE POSSIBILITY OF SUCH DAMAGE.                                         
  32.  *                                                                             
  33.  * THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT       
  34.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A    
  35.  * PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"       
  36.  * BASIS, AND THE AUTHOR HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,  
  37.  * UPDATES, ENHANCEMENTS, OR MODIFICATIONS.                                   
  38.  *                                                                             
  39.  *                                                                             
  40.  * Copyright 1992 by David A. Curry                                            
  41.  *                                                                             
  42.  * Permission to use, copy, modify, distribute, and sell this software and its 
  43.  * documentation for any purpose is hereby granted without fee, provided that  
  44.  * the above copyright notice appear in all copies and that both that copyright
  45.  * notice and this permission notice appear in supporting documentation.  The  
  46.  * author makes no representations about the suitability of this software for  
  47.  * any purpose.  It is provided "as is" without express or implied warranty.   
  48.  *                                                                             
  49.  * David A. Curry, N9MSW                                                       
  50.  * Purdue University                                                           
  51.  * Engineering Computer Network                                                
  52.  * 1285 Electrical Engineering Building                                        
  53.  * West Lafayette, IN 47907                                                    
  54.  * davy@ecn.purdue.edu                                                         
  55.  *                                                                             
  56.  * VersaTrack Copyright (c) 1993, 1994 Siamack Navabpour. All Rights Reserved.
  57.  *
  58.  * Permission is hereby granted to copy, modify and distribute VersaTrack
  59.  * in whole, or in part, for educational, non-profit and non-commercial use
  60.  * only, free of charge or obligation, and without agreement, provided that
  61.  * all copyrights and restrictions noted herein are observed and followed, and
  62.  * additionally, that this and all other copyright notices listed herein
  63.  * appear unaltered in all copies and in all derived work.
  64.  *
  65.  * This notice shall not in any way void or supersede any of the other authors
  66.  * rights or privileges.
  67.  *
  68.  * VersaTrack IS PRESENTED FREE AND "AS IS", WITHOUT ANY WARRANTY OR SUPPORT.
  69.  * YOU USE IT AT YOUR OWN RISK. The author(s) shall not be liable for any
  70.  * direct, indirect, incidental, or consequential damage, loss of profits or
  71.  * other tangible or intangible losses or benefits, arising out of or related
  72.  * to its use. VersaTrack carries no warranty, explicit or implied, including
  73.  * but not limited to those of merchantablity and fitness for a particular
  74.  * purpose.
  75.  *
  76.  * Siamack Navabpour, 12342 Hunter's Chase Dr. Apt. 2114, Austin, TX 78729.
  77.  * sia@bga.com or sia@realtime.com.
  78.  */
  79.  
  80.  
  81. #include <windows.h>
  82. #include <stdio.h>
  83. #include <math.h>
  84. #include <time.h>
  85. #include <string.h>
  86. #include <stdlib.h>
  87.  
  88. #include "vstdefs.h"
  89. #include "vsttype.h"
  90. #include "libxtrns.h"
  91.  
  92. static
  93. int   month_days[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  94.  
  95. long day_number(year,month,day)
  96. int year, month, day;
  97. {
  98.     long result;
  99.     
  100.     if (year < 50)                 /* allows 4 or 2 digit year specifications */
  101.         year += 2000;
  102.     else
  103.         if (year < 100)
  104.             year += 1900;
  105.  
  106.     result = ((((long) year - 1901) * 1461) >> 2) 
  107.              + month_days[month-1] + day + 365;
  108.  
  109.     if (year%4 == 0 && month > 2)         /* correct day number for leap year */
  110.         result++;
  111.  
  112.     return result;
  113. }
  114.  
  115. static void
  116. daytoymdyd(daynum, year, month, day, yearday)
  117. long daynum;
  118. int  *year, *month, *day, *yearday;
  119.  
  120. {
  121.     long Y;
  122.     int  M, L;
  123.  
  124.     Y         = 4 * daynum;
  125.     Y        /= 1461;
  126.     daynum   -= 365 + (((Y - 1) * 1461) >> 2);
  127.     *yearday  = daynum;
  128.     L = 0;
  129.     if (Y%4 == 0 && daynum > month_days[2])
  130.         L = 1;
  131.     M = 1;
  132.     while (daynum > month_days[M] + L)
  133.         M++;
  134.     daynum -= (month_days[M-1]);
  135.     if (M > 2)
  136.         daynum -= L;
  137.    
  138.     *year  = Y + 1900;
  139.     *month = M;
  140.     *day   = daynum;
  141. }
  142.  
  143. double
  144. utctime()
  145. {
  146.     double daynum;
  147.     double hour;
  148.     SYSTEMTIME tm;
  149.  
  150.     GetSystemTime(&tm);
  151.  
  152.     daynum    = day_number(tm.wYear, tm.wMonth, tm.wDay);
  153.     hour      = (double) tm.wHour + (double)tm.wMinute / 60.0 +
  154.                 (double) tm.wSecond/3600.0 + (double)tm.wMilliseconds / (3600.0 * 1000.0);
  155.     return daynum + hour/24.0;
  156. }
  157.  
  158. void UTCtimeStr(time,str)
  159. double time;
  160. char *str;
  161. {
  162.     double dayTime;
  163.     long   dayNum;
  164.     int    hours, mins, secs, dummyi;
  165.  
  166.     dayNum  = (long) time;
  167.     dayTime = time - (double) dayNum;
  168.     timetodhms(dayTime, &dummyi, &hours, &mins, &secs);
  169.     sprintf(str,"%02d:%02d:%02d", hours, mins, secs);
  170. }
  171.  
  172.  
  173. static void
  174. timetodhms(time, days, hours, mins, secs)
  175. double time;
  176. int    *days, *hours, *mins, *secs;
  177.  
  178. {
  179.     double timed, timeh, timem, times;
  180.  
  181.     timed  = time;
  182.     timeh  = 24.0 *   modf(timed,&timed);
  183.     timem  = 60.0 *   modf(timeh,&timeh);
  184.     times  = 60.0 *   modf(timem,&timem);
  185.     times +=          modf(times,×);
  186.  
  187.     if (fabs(times - 60.0) < 5.0e-4) {
  188.         times  = 0.0;
  189.         timem += 1.0;
  190.         if (fabs(timem - 60.0) < 1.0e-4) {
  191.             timem  = 0.0;
  192.             timeh += 1.0;
  193.             if (fabs(timeh - 24.0) < 1.0e-4) {
  194.                 timeh  = 0.0;
  195.                 timed += 1.0;
  196.             }
  197.         }
  198.     }
  199.     *days  = (int) timed;
  200.     *hours = (int) timeh;
  201.     *mins  = (int) timem;
  202.     *secs  = (int) times;
  203. }
  204.  
  205.  
  206. void
  207. UTCdateStr(time, string)
  208. double time;  /* in day units + fractions */
  209. char *string;
  210. {
  211.     double dayTime;
  212.     long dayNum;
  213.     int  day, month, year, yearDay, hours, mins, secs, idummy;
  214.  
  215.     dayNum = (long) time;
  216.     daytoymdyd(dayNum, &year, &month, &day, &yearDay);
  217.     dayTime = time - (double) dayNum;
  218.     timetodhms(dayTime, &idummy, &hours, &mins, &secs);
  219.  
  220.     sprintf(string, "%02d/%02d/%02d %02d:%02d:%02d", month, day, year-1900,
  221.         hours, mins, secs);
  222. }
  223.  
  224.  
  225. char *monthname(month)
  226. int  month;
  227. {
  228.     static char *name[] = { "???", "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  229.                             "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  230.  
  231.     return (month < 1 || month > 12) ? name[0] : name[month];
  232. }
  233.  
  234.  
  235. double internalTime( yr, mon, day, hour, min, sec)
  236. int yr, mon, day, hour, min, sec;
  237. {
  238.     double t, h;
  239.     
  240.     t  = (double) day_number(yr, mon, day);
  241.     h  = (double) hour + (double) min / 60.0 + (double)sec / 3600.0;
  242.     t += h / 24.0;
  243.     
  244.     return t;
  245. }
  246.  
  247.  
  248.  
  249.